package zozopo.log.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class UserLoginLogOS extends OperatingService{
	
	int securityMaxNum = 240;
	int maxTimes = 10;
	
	public UserLoginLogOS(Connection conn){
		super(conn);
	}
	
	public UserLoginLogOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<UserLoginLogTB> selectListBySql(String sql){
		List<UserLoginLogTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					list = new ArrayList<UserLoginLogTB>();
					while(resultSet.next()){
						UserLoginLogTB obj = new UserLoginLogTB();
						
						obj.setId(resultSet.getInt(UserLoginLogTB.ID));
						obj.setIp(resultSet.getString(UserLoginLogTB.IP));
						obj.setAccount(resultSet.getString(UserLoginLogTB.ACCOUNT));
						obj.setIswrong(resultSet.getBoolean(UserLoginLogTB.ISWRONG));
						obj.setLogindate(resultSet.getString(OperatorLoginLogTB.LOGINDATE));
					
				
						list.add(obj);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(UserLoginLogTB userLoginLogTB){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();

		insertMap.put(UserLoginLogTB.IP, userLoginLogTB.getIp());
		insertMap.put(UserLoginLogTB.ISWRONG, userLoginLogTB.isIswrong());
		insertMap.put(UserLoginLogTB.USERID, userLoginLogTB.getUserid());
		insertMap.put(UserLoginLogTB.ACCOUNT, userLoginLogTB.getAccount());
		insertMap.put(UserLoginLogTB.TYPE, userLoginLogTB.getType());
		
		return sqlMachine.insertRow(UserLoginLogTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean isWorngOutOfTime(String account){
		boolean isWorngOutOfTime = false;
		
		int worngTime = 0; 
		
		
		String sql = "SELECT COUNT(account) as worngTime FROM "+UserLoginLogTB.TABLE_NAME+" WHERE "+UserLoginLogTB.ISWRONG+"=TRUE AND account='"+account+"' AND  logindate >= DATE_ADD(NOW(), INTERVAL -1 DAY)" ;
		
		
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					worngTime = resultSet.getInt("worngTime");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(worngTime > maxTimes){
			return true;
		}
		return isWorngOutOfTime;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean isAccountSecurity(String account){
		boolean isAccountSecurity = false;
		
		int worngTime = 0;
		String sql = "SELECT COUNT(account) as worngTime FROM "+UserLoginLogTB.TABLE_NAME+" WHERE "+UserLoginLogTB.ISWRONG+"=TRUE AND account='"+account+"' AND  logindate >= DATE_ADD(NOW(), INTERVAL -1 DAY)" ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					worngTime = resultSet.getInt("worngTime");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(worngTime < securityMaxNum){
			isAccountSecurity = true;
		}
		return isAccountSecurity;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public UserLoginLogTB getNewLoginInfo(String account){
		UserLoginLogTB userLoginLogTB = new UserLoginLogTB();
		

		String sql = "SELECT * FROM "+UserLoginLogTB.TABLE_NAME+" WHERE "+UserLoginLogTB.ISWRONG+"=FALSE ORDER BY "+UserLoginLogTB.LOGINDATE+" DESC LIMIT 0,1" ;
		
		List<UserLoginLogTB> list = selectListBySql(sql);
		if(list == null){
			return userLoginLogTB;
		}
		if(list.size() < 1){
			return userLoginLogTB;
		}
		
		userLoginLogTB = list.get(0);
		
		return userLoginLogTB;
	}

}
